/**
 * 
 */
package com.heer.RD.projecthour.page.include;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import com.heer.RD.auth.HeerSession;
import com.heer.RD.projecthour.page.BasePage;
import com.heer.RD.projecthour.page.LoginPage;
import com.heer.RD.projecthour.page.MainPage;
import com.heer.RD.projecthour.page.PersonStatPage;
import com.heer.RD.projecthour.page.PersonWeekStatPage;
import com.heer.RD.projecthour.page.ProjectMngPage;
import com.heer.RD.projecthour.page.ProjectStatPage;
import com.heer.RD.projecthour.page.SysMngPage;
import com.heer.RD.util.SecurityFacade;

/**
 * @author ZhaoZehui
 *
 */
public class NavPanel extends Panel {

	public NavPanel(String id, final WebPage page) {
		super(id);
		
		List<NavMenu> menuList = new ArrayList<NavMenu>();
		if(HeerSession.session().isSignIn()) {
			String userRole = ((BasePage)page).getUser().getUserRole();
			
			if(SecurityFacade.isStaff()) {
				menuList.add(new NavMenu("个人工时登记",MainPage.class));
				menuList.add(new NavMenu("个人工时统计(按天)",PersonWeekStatPage.class));
				menuList.add(new NavMenu("个人工时统计(合计)",PersonStatPage.class));
			}
			if(SecurityFacade.isPM() || SecurityFacade.isManager()) {
				menuList.add(new NavMenu("项目工时统计",ProjectStatPage.class));
				menuList.add(new NavMenu("项目维护",ProjectMngPage.class));
			}
			if(SecurityFacade.isAdmin()) {
				menuList.add(new NavMenu("系统管理",SysMngPage.class));
			}
		}
		else {
			menuList.add(new NavMenu("登录/注册",LoginPage.class));
		}
		
		add(new ListView("navMenu", menuList) {
			protected void populateItem(ListItem item) {
				NavMenu menu = (NavMenu)item.getModel().getObject();
				
				String menuId = menu.getPageClazz().getName().replaceAll("\\.", "_");
				item.setMarkupId(menuId);
				item.setOutputMarkupId(true);
				
				if(menu.getPageClazz().isAssignableFrom(page.getClass()))  {
					item.add(new AttributeModifier("class", true, new Model<String>("active")));
				}
				
				Link menuLink = new PageLink("menuLink", menu.getPageClazz());
				menuLink.setMarkupId(menuId+"_link");
				menuLink.setOutputMarkupId(true);
				
				Label menuName = new Label("menuName", menu.getName());
				menuName.setMarkupId(menuId+"_label");
				menuName.setOutputMarkupId(true);
				
				menuLink.add(menuName);
				item.add(menuLink);
			}
			
		});

	}
	
	public class NavMenu implements Serializable{
		private String name;
		private Class pageClazz;
		
		public NavMenu() {
		}
		public NavMenu(String name, Class pageClazz) {
			this.name = name;
			this.pageClazz = pageClazz;
		}
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Class getPageClazz() {
			return pageClazz;
		}
		public void setPageClazz(Class pageClazz) {
			this.pageClazz = pageClazz;
		}
	}

}
